package siga.visao_busca;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import siga.banco.conexao;
import siga.visao.index;
import siga.visao.verErros;

public class buscaCpaAvaliacao extends javax.swing.JDialog {
    public static boolean bcpaa_finaliza = false;
    public static List bcpaa_dados = new ArrayList();

    public buscaCpaAvaliacao(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpBCPrincipal = new javax.swing.JPanel();
        jpBusca = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtfCodigo = new javax.swing.JTextField();
        jtfDescricao = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jpTabela = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtAvaliacao = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Buscar Avaliação");

        jpBCPrincipal.setName("jpBCPrincipal"); // NOI18N

        jpBusca.setName("jpBusca"); // NOI18N

        jLabel1.setFont(new java.awt.Font("SansSerif", 0, 11));
        jLabel1.setText("Código da Av.:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(new java.awt.Font("SansSerif", 0, 11));
        jLabel2.setText("Avaliação:");
        jLabel2.setName("jLabel2"); // NOI18N

        jtfCodigo.setFont(new java.awt.Font("SansSerif", 0, 11));
        jtfCodigo.setName("jtfCodigo"); // NOI18N
        jtfCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfCodigoKeyPressed(evt);
            }
        });

        jtfDescricao.setFont(new java.awt.Font("SansSerif", 0, 11));
        jtfDescricao.setName("jtfDescricao"); // NOI18N
        jtfDescricao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfDescricaoKeyPressed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_16x16.png"))); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpBuscaLayout = new javax.swing.GroupLayout(jpBusca);
        jpBusca.setLayout(jpBuscaLayout);
        jpBuscaLayout.setHorizontalGroup(
            jpBuscaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpBuscaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpBuscaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(10, 10, 10)
                .addGroup(jpBuscaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpBuscaLayout.createSequentialGroup()
                        .addComponent(jtfDescricao, javax.swing.GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jLabel2))
                .addContainerGap())
        );
        jpBuscaLayout.setVerticalGroup(
            jpBuscaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpBuscaLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jpBuscaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addGroup(jpBuscaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jpBuscaLayout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jtfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jpBuscaLayout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jtfDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jpTabela.setName("jpTabela"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jtAvaliacao.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Avaliação"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtAvaliacao.setName("jtAvaliacao"); // NOI18N
        jtAvaliacao.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtAvaliacaoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtAvaliacao);
        jtAvaliacao.getColumnModel().getColumn(0).setPreferredWidth(100);
        jtAvaliacao.getColumnModel().getColumn(0).setMaxWidth(100);

        javax.swing.GroupLayout jpTabelaLayout = new javax.swing.GroupLayout(jpTabela);
        jpTabela.setLayout(jpTabelaLayout);
        jpTabelaLayout.setHorizontalGroup(
            jpTabelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 671, Short.MAX_VALUE)
        );
        jpTabelaLayout.setVerticalGroup(
            jpTabelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jpBCPrincipalLayout = new javax.swing.GroupLayout(jpBCPrincipal);
        jpBCPrincipal.setLayout(jpBCPrincipalLayout);
        jpBCPrincipalLayout.setHorizontalGroup(
            jpBCPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpBusca, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jpTabela, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jpBCPrincipalLayout.setVerticalGroup(
            jpBCPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpBCPrincipalLayout.createSequentialGroup()
                .addComponent(jpBusca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpTabela, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpBCPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpBCPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        busca();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jtAvaliacaoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtAvaliacaoMouseClicked
       if(evt.getClickCount() == 2){
            bcpaa_finaliza = true;
            bcpaa_dados.add(0,jtAvaliacao.getValueAt(jtAvaliacao.getSelectedRow(), 0).toString());
            bcpaa_dados.add(1,jtAvaliacao.getValueAt(jtAvaliacao.getSelectedRow(), 1).toString());
            setVisible(false);
            dispose();
        }
    }//GEN-LAST:event_jtAvaliacaoMouseClicked

    private void jtfCodigoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfCodigoKeyPressed
        if(evt.getKeyCode()==10){
            busca();
        }
    }//GEN-LAST:event_jtfCodigoKeyPressed

    private void jtfDescricaoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfDescricaoKeyPressed
        if(evt.getKeyCode()==10){
            busca();
        }
    }//GEN-LAST:event_jtfDescricaoKeyPressed

    private void busca(){
        int i = jtAvaliacao.getRowCount();
        String txtsql;
        javax.swing.table.DefaultTableModel jtm = (javax.swing.table.DefaultTableModel)jtAvaliacao.getModel();
        for(int x=(i-1);x>=0; --x ){
            jtm.removeRow(x);
        }
        try {
            if(!jtfCodigo.getText().isEmpty()){
                txtsql = "and codigo="+jtfCodigo.getText();
            }else if(!jtfDescricao.getText().isEmpty()){
                txtsql = "and descricao like \""+jtfDescricao.getText()+"%\"";
            }else{
               txtsql = "";
            }
            conexao.Query("select codigo, descricao from cpa_avaliacao where cod_emp="+index.cod_emp+" "+txtsql+" order by descricao");
            while (conexao.rs.next()) {
                jtm.addRow(new Object[]{conexao.rs.getString("codigo"),conexao.rs.getString("descricao")});
            }
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel jpBCPrincipal;
    private javax.swing.JPanel jpBusca;
    private javax.swing.JPanel jpTabela;
    private javax.swing.JTable jtAvaliacao;
    private javax.swing.JTextField jtfCodigo;
    private javax.swing.JTextField jtfDescricao;
    // End of variables declaration//GEN-END:variables

}
